/*!
 * negotiator
 * https://github.com/jshttp/negotiator
 */
import preferredCharsets from './charset'
import preferredEncodings from './encoding'
import preferredLanguages from './language'
import preferredMediaTypes from './mediaType'

/**
 * Create a Negotiator instance from a request.
 * @param {object} request
 * @public
 */
export default function Negotiator(request) {
  if(!(this instanceof Negotiator)) {
    return new Negotiator(request)
  }

  this.request = request
}

Negotiator.prototype.charset = function charset(available) {
  var set = this.charsets(available)
  return set && set[0]
}

Negotiator.prototype.charsets = function charsets(available) {
  return preferredCharsets(this.request.headers['accept-charset'], available)
}

Negotiator.prototype.encoding = function encoding(available) {
  var set = this.encodings(available)
  return set && set[0]
}

Negotiator.prototype.encodings = function encodings(available) {
  return preferredEncodings(this.request.headers['accept-encoding'], available)
}

Negotiator.prototype.language = function language(available) {
  var set = this.languages(available)
  return set && set[0]
}

Negotiator.prototype.languages = function languages(available) {
  return preferredLanguages(this.request.headers['accept-language'], available)
}

Negotiator.prototype.mediaType = function mediaType(available) {
  var set = this.mediaTypes(available)
  return set && set[0]
}

Negotiator.prototype.mediaTypes = function mediaTypes(available) {
  return preferredMediaTypes(this.request.headers.accept, available)
}

Negotiator.prototype.preferredCharset = Negotiator.prototype.charset
Negotiator.prototype.preferredCharsets = Negotiator.prototype.charsets
Negotiator.prototype.preferredEncoding = Negotiator.prototype.encoding
Negotiator.prototype.preferredEncodings = Negotiator.prototype.encodings
Negotiator.prototype.preferredLanguage = Negotiator.prototype.language
Negotiator.prototype.preferredLanguages = Negotiator.prototype.languages
Negotiator.prototype.preferredMediaType = Negotiator.prototype.mediaType
Negotiator.prototype.preferredMediaTypes = Negotiator.prototype.mediaTypes
